package com.jyu.hakkatranslation.service.Impl;

import com.jyu.hakkatranslation.dao.UserMapper;
import com.jyu.hakkatranslation.pojo.User;
import com.jyu.hakkatranslation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User login(String username, String password) {
        return userMapper.login(username, password);
    }

    @Override
    public int queryByUsername(String username) {
        User user = userMapper.searchUsername(username);
        if (user==null){
            return 0;
        }
        else {
        return 1;
        }
    }

    @Override
    public void register(String username, String password,String email) {
        userMapper.saveUser(username,password,email);
    }

//    @Override
//    public void register(User user) {
//        //根据用户输入判断用户名是否存在
//        User user1 = userMapper.searchUsername(user.getUsername());
//        if(!ObjectUtils.isEmpty(user1)) throw new RuntimeException("当前用户已被注册");
//        //注册用户
//        userMapper.saveUser(user);
//    }




//    @Autowired
//    private UserMapper userMapper;
//
//    /**
//     * 注册
//     * @param user
//     * @return
//     */
//    @Override
//    public boolean register(User user) {
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("username",user.getUsername());
//        User user1 = userMapper.selectOne(queryWrapper);
//        if(user!=null){//如果已经存在
//            return false;
//        }else {
//            int insert = userMapper.insert(user1);
//            return true;
//        }
//    }
//
//    /**
//     * 登录功能
//     * @param username
//     * @param password
//     * @return
//     */
//    @Override
//    public User login(String username, String password) {
//        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("username",username)
//                .eq("password",password);
//        User user = userMapper.selectOne(queryWrapper);
//        return user;
//    }
}
